import {
  getOrgs,
  addOrg,
  modifyOrg,
  removeOrg,
  getOrgUsers,
  addOrgUsers,
  removeOrgUsers,
  getDatasourceListByOrgId,
  removeOrgDatasource,
  addOrgDatasource,
  updateOrgDatasource
} from "@/api/orgManage"
import {
  getUserListApi
} from "@/api/userManage"

const orgManage  = {
  namespaced: true,
  state: {
    orgTreeData: [],
    replaceFields: {
      children:'children',
      title:'name',
      key:'id',
      value: 'id'
    },
    orgUserTableData: [],
    paginationConfig: {
      current : 1,
      pageSize: 10,
      total: 0,
      pageSizeOptions: ['10', '30', '50', '100'],
      showQuickJumper: true,
      showSizeChanger: true,
      showTotal : (total)=>{
        let pageConfig = orgManage.state.paginationConfig
        return `共  ${total}  条记录   第  ${pageConfig.current} / ${Math.ceil(pageConfig.total/pageConfig.pageSize)}  页 `
      }
    },
    permissionPaginationConfig: {
      current : 1,
      pageSize: 10,
      total: 0,
      pageSizeOptions: ['10', '30', '50', '100'],
      showQuickJumper: true,
      showSizeChanger: true,
      order: 'desc',
      orderBy: 'updatedAt',
      showTotal : (total)=>{
        let pageConfig = orgManage.state.permissionPaginationConfig
        return `共  ${total}  条记录   第  ${pageConfig.current} / ${Math.ceil(pageConfig.total/pageConfig.pageSize)}  页 `
      },
    },
    order: 'desc',
    orderBy: 'createdAt',
    allUsers: []
  },
  mutations: {
    setOrgTreeData: (state, data) => {
      state.orgTreeData = data
    },
    setOrgUserTableData: (state, data) => {
      state.orgUserTableData = data
    },
    setOrgUserList: (state, data) => {
      state.orgUserList = data
    },
    setTotal: (state, total) => {
      state.paginationConfig.total = total
    },
    setPage: (state, current) => {
      state.paginationConfig.current = current
    },
    setPageSize: (state, pageSize) => {
      state.paginationConfig.pageSize = pageSize
    },
    setOrder: (state, order) => {
      state.order = order
    },
    setOrderBy: (state, orderBy) => {
      state.orderBy = orderBy
    },
    setAllUsers: (state, data) => {
      state.allUsers = data
    },
    setPermissionTableTotal: (state, total) => {
      state.permissionPaginationConfig.total = total
    },
    setPermissionTablePage: (state, current) => {
      state.permissionPaginationConfig.current = current
    },
    setPermissionTablePageSize: (state, pageSize) => {
      state.permissionPaginationConfig.pageSize = pageSize
    },
  },
  actions: {
    getOrgData(context) {
      return new Promise((resolve, reject) => {
        getOrgs().then(res => {
          if(res.status) {
            const data = res.data
            context.commit("setOrgTreeData", data)
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    addOrg(context, data) {
      return new Promise((resolve, reject) => {
        addOrg(data)
          .then(res => {
            if(res.status) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            reject(error)
          })
      })
    },
    modifyOrg(context, data) {
      return new Promise((resolve, reject) => {
        modifyOrg(data.orgId, data.data)
          .then(res => {
            if (res.status) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
          reject(error)
        })
      })
    },
    removeOrg(context, orgId) {
      return new Promise((resolve, reject) => {
        removeOrg(orgId)
          .then(res => {
            if (res.status) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
          reject(error)
        })
      })
    },
    getOrgUserList(context, orgId) {
      return new Promise((resolve, reject) => {
        const params = {
          page: context.state.paginationConfig.current,
          pageSize: context.state.paginationConfig.pageSize,
          order: context.state.order,
          orderBy: context.state.orderBy
        }
        getOrgUsers(orgId, params)
          .then(res => {
            if(res.status) {
              const data = res.data.data
              context.commit("setOrgUserTableData", data)
              context.commit("setTotal", res.data.total)
              context.commit("setPage", res.data.current)
              context.commit("setPageSize", res.data.size)
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
          reject(error)
        })
      })
    },
    getAllUsersList(context) {
      return new Promise((resolve, reject) => {
        const params = {
          page: 1,
          pageSize: 1000,
          order: 'asc',
          orderBy: 'username'
        }
        getUserListApi(params)
          .then(res => {
            if(res.status) {
              const data = res.data.data
              context.commit("setAllUsers", data)
              resolve(data)
            } else {
              reject(res)
            }
          }).catch(error => {
            reject(error)
          })
      })
    },
    addOrgUsers(context, data) {
      return new Promise((resolve, reject) => {
        addOrgUsers(data.orgId, { userIds: data.userIds.join(",")})
          .then(res => {
            if(res.status) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            reject(error)
          })
      })
    },
    removeOrgUsers(context, data) {
      return new Promise((resolve, reject) => {
        removeOrgUsers(data.orgId, { userIds: data.userIds.join(",")})
          .then(res => {
            if(res.status) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
          reject(error)
        })
      })
    },
    getPermissionListByOrgId(context, data) {
      const pageSize = data.pageSize ? data.pageSize:context.state.permissionPaginationConfig.pageSize
      const page = data.page ? data.page : context.state.permissionPaginationConfig.current
      const order = data.order ? data.order : context.state.permissionPaginationConfig.order
      const orderBy = data.orderBy ? data.orderBy : context.state.permissionPaginationConfig.orderBy
      return new Promise((resolve, reject) => {
        getDatasourceListByOrgId(data.orgId, {
            page: page,
            pageSize: pageSize,
            order: order,
            orderBy: orderBy
        }).then(res => {
            if(res.status) {
              context.commit("setPermissionTableTotal", res.data.total)
              context.commit("setPermissionTablePage", res.data.current)
              context.commit("setPermissionTablePageSize", res.data.size)
              resolve(res.data.data)
            } else {
              reject(res)
            }
          }).catch(error => {
          reject(error)
        })
      })
    },
    removeOrgPermission(context, data) {
      return new Promise((resolve, reject) => {
        removeOrgDatasource(data.orgId, data.datasourceId)
          .then(res => {
            if(res.status) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            console.error(error)
            reject(error)
        })
      })
    },
    addOrgPermission(context,data){
      return new Promise((resolve, reject) => {
        const orgId = data.orgId
        delete data.orgId
        addOrgDatasource(orgId, data)
          .then(res => {
            if(res.status) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            console.error(error)
            reject(error)
        })
      })
    },
    updateOrgPermission(context, data) {
      return new Promise((resolve, reject) => {
        const orgId = data.orgId
        delete data.orgId
        const datasourceId = data.datasourceId
        delete data.datasourceId
        updateOrgDatasource(orgId, datasourceId, data)
          .then(res => {
            if(res.status) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
          reject(error)
        })
      })
    }
  },

  getters: {

  }
}

export default orgManage
